package indigo.captcha.image;

import java.security.SecureRandom;
import java.util.Random;

import com.jhlabs.image.RGBAdjustFilter;

public class RandomDarkenDeformer extends ThreadLocalFilterDeformer<RGBAdjustFilter> {
	private final Random random = new SecureRandom();
	private final float minFactor;
	private final float maxFactor;

	public RandomDarkenDeformer(float minFactor, float maxFactor) {
		this.minFactor = minFactor;
		this.maxFactor = maxFactor;
	}

	public RandomDarkenDeformer() {
		this(0f, 1f);
	}

	@Override
	protected RGBAdjustFilter createFilter() {
		return new RGBAdjustFilter();
	}

	@Override
	protected void prepareFilter(RGBAdjustFilter filter) {
		filter.setRFactor(-minFactor - random.nextFloat() * (maxFactor - minFactor));
		filter.setGFactor(-minFactor - random.nextFloat() * (maxFactor - minFactor));
		filter.setBFactor(-minFactor - random.nextFloat() * (maxFactor - minFactor));
	}
}
